package cn.bugstack.trigger.listener;

import cn.bugstack.domain.auth.service.WechatMessageService;
import cn.bugstack.domain.goods.service.IGoodsService;
import cn.bugstack.domain.order.adapter.event.PaySuccessMessageEvent;
import cn.bugstack.domain.order.adapter.repository.IOrderRepository;
import cn.bugstack.domain.order.model.entity.OrderEntity;
import com.alibaba.fastjson.JSON;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 监听支付系统，监听到支付宝支付成功消息后，完成发货更新订单状态等动作。
 * @description 支付成功回调消息
 */
@Slf4j
@Component
public class OrderPaySuccessListener {

    @Resource
    private IGoodsService goodsService;

    @Resource
    private WechatMessageService wechatMessageService;

    // @Subscribe - 旧版发布订阅方式
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "${spring.rabbitmq.config.consumer.topic_order_pay_success.queue}"),
                    exchange = @Exchange(value = "${spring.rabbitmq.config.consumer.topic_order_pay_success.exchange}", type = ExchangeTypes.TOPIC),
                    key = "${spring.rabbitmq.config.consumer.topic_order_pay_success.routing_key}"
            )
    )
    public void listener(String paySuccessMessageJson) {
        try {
            // 非营销：收到支付宝支付成功的消息。营销：拼团回调
            log.info("收到支付成功消息 {}", paySuccessMessageJson);

            PaySuccessMessageEvent.PaySuccessMessage paySuccessMessage = JSON.parseObject(paySuccessMessageJson, PaySuccessMessageEvent.PaySuccessMessage.class);

            log.info("模拟发货（如；发货、充值、开户员、返利），单号:{}", paySuccessMessage.getTradeNo());

            // 变更订单状态 - 发货完成&结算
            goodsService.changeOrderDealDone(paySuccessMessage.getTradeNo());

            // 查询订单信息并发送微信发货消息
            try {
                wechatMessageService.sendDeliveryMessage(
                        paySuccessMessage.getUserId(),
                        paySuccessMessage.getTradeNo(),
                        // TODO 写死商品名称
                        "Mybatis"
                );
            } catch (Exception e) {
                log.error("发送微信发货消息失败 userId:{}, orderNo:{}",
                        paySuccessMessage.getUserId(), paySuccessMessage.getTradeNo(), e);
                // 微信消息发送失败不影响主流程
            }

            // 可以打开测试，MQ 消费失败，会抛异常，之后重试消费。这个也是最终执行的重要手段。
            // throw new RuntimeException("重试消费");
        } catch (Exception e) {
            log.error("收到支付成功消息失败 {}", paySuccessMessageJson, e);
            throw e;
        }
    }
}
